package com.ddwl.shop.service;

import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.shop.QueryShopBannerBo;
import com.ddwl.schema.bo.shop.ShopBannerBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.shop.ShopBannerVo;
import com.ddwl.shop.dao.ShopBannerDao;
import com.ddwl.shop.dao.model.ShopBanner;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ShopBannerService {

    private final ShopBannerDao shopBannerDao;

    @Transactional(rollbackFor = Throwable.class)
    public void create(ShopBannerBo bo, String userId, String shopId) {
        ParamException.isNotNull(bo.getId(),"id应该为空");
        bo.setShopId(shopId);
        ParamException.isBlank(bo.getImgUrl(),"图片为空");
        ShopBanner shopBanner = BeanConverUtils.convertBean(bo, ShopBanner.class);
        shopBanner.setCreatedUserId(userId);
        shopBanner.setEffect(0);
        shopBannerDao.insert(shopBanner);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void wbmCreate(ShopBannerBo bo,String userId,String shopId) {
        shopBannerDao.deleteByShopId(userId,shopId);
        ParamException.isNotNull(bo.getId(),"id应该为空");
        ParamException.isBlank(bo.getImgUrl(),"图片为空");
        String[] split = bo.getImgUrl().split(",");
        for (String url:split) {
            ShopBanner shopBanner=new ShopBanner();
            shopBanner.setImgUrl(url);
            shopBanner.setSort(0);
            shopBanner.setEffect(1);
            shopBanner.setCreatedUserId(userId);
            shopBanner.setShopId(shopId);
            shopBanner.setTitle(UUIDGenerator.getShortUUID());
            shopBannerDao.insert(shopBanner);
        }
    }



    @Transactional(rollbackFor = Throwable.class)
    public void update(ShopBannerBo bo,String userId,String shopId) {
        ParamException.isBlank(bo.getId(),"id为空");
        ShopBanner shopBanner = BeanConverUtils.convertBean(bo, ShopBanner.class);
        shopBanner.setModifiedUserId(userId);
        shopBannerDao.updateShopBanner(shopBanner,shopId);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void delete(String id,String userId,String shopId) {
        ParamException.isNull(id,"id为空");
        shopBannerDao.delete(id,userId,shopId);
    }

    public PageVo<ShopBannerVo> page(PageBo<QueryShopBannerBo> boPageBo, String shopId) {
        boPageBo.getParam().setShopId(shopId);
        PageHelper.startPage(boPageBo.getPage(),boPageBo.getPageSize(), MySqlUtil.orderBy(boPageBo.getSort(),boPageBo.isAsc()));
        PageInfo<ShopBanner> info = new PageInfo(shopBannerDao.queryShopBannerList(boPageBo.getParam()));
        List<ShopBannerVo> list = BeanConverUtils.convertBeanList(info.getList(),ShopBannerVo.class);
        return new PageVo<>(info.getTotal(),boPageBo.getPage(),boPageBo.getPageSize(),list);
    }

    public ShopBannerVo getById(String id) {
        ParamException.isNull(id,"id为空");
        ShopBanner shopBanner=new ShopBanner();
        shopBanner.setId(id);
        shopBanner.setEffect(1);
        ShopBannerVo shopBannerVo = BeanConverUtils.convertBean(shopBannerDao.selectOne(shopBanner),ShopBannerVo.class);
        return shopBannerVo;
    }

    public List<ShopBannerVo> getBanner(String shopId) {
        ParamException.isNull(shopId,"门店为空");
        ShopBanner shopBanner=new ShopBanner();
        shopBanner.setShopId(shopId);
        shopBanner.setEffect(1);
        List<ShopBannerVo> shopBannerVo = BeanConverUtils.convertBeanList(shopBannerDao.select(shopBanner),ShopBannerVo.class);
        return shopBannerVo;
    }

    public ShopBannerVo getByShopId(QueryShopBannerBo bo,String shopId) {
        bo.setShopId(shopId);
        ShopBanner shopBanner=new ShopBanner();
        shopBanner.setEffect(1);
        shopBanner.setShopId(bo.getShopId());
        shopBanner.setPosition(bo.getPosition());
        ShopBannerVo shopBannerVo = BeanConverUtils.convertBean(shopBannerDao.selectOne(shopBanner),ShopBannerVo.class);
        return shopBannerVo;
    }

    public void effect(Integer effect, String userIdNew, String shopId,String bannerId) {
        ParamException.isBlank(shopId,"id为空");
        ParamException.isBlank(bannerId,"bannerId为空");
        ShopBanner shopBanner = new ShopBanner();
        shopBanner.setModifiedUserId(userIdNew);
        shopBanner.setEffect(effect);
        shopBanner.setId(bannerId);
        shopBannerDao.updateShopBanner(shopBanner,shopId);
    }
}
